import { login } from '@/api/user'
import { setToken, removeToken, getToken } from '@/utils/auth'

export default {
  namespaced: true,
  state: {
    token: getToken()
  },
  actions: {
    async login(context, data) {
      const token = await login(data)
      context.commit('setToken', token)
    }
  },
  mutations: {
    // 存储token
    setToken(state, newToken) {
      state.token = newToken
      setToken(newToken)
    },
    // 删除token
    removeToken(state) {
      // 删除Vuex的token
      state.token = null
      removeToken()
    }

  }

}
